using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QualityDisplay : MonoBehaviour
{
	public UIWidget thumb;

	public UIWidget container;

	public UISprite layerPrefab;

	public Vector3 startScale = new Vector3(2f, 2f, 1f);

	public float startAlpha;

	public float thumbDuration = 0.25f;

	public float endAlpha = 0.4f;

	protected List<UISprite> layers = new List<UISprite>();

	private void Awake()
	{
		SetRanges(0f, 0f, 200f, false);
	}

	private void OnEnable()
	{
		int a = Mathf.Abs(container.leftAnchor.absolute);
		int b = Mathf.Abs(container.rightAnchor.absolute);
		container.leftAnchor.relative = ((GGPlayerSettings.instance.Model.handedness != 0) ? 1 : 0);
		container.rightAnchor.relative = ((GGPlayerSettings.instance.Model.handedness != 0) ? 1 : 0);
		container.leftAnchor.absolute = ((GGPlayerSettings.instance.Model.handedness != 0) ? (-Mathf.Max(a, b)) : Mathf.Min(a, b));
		container.rightAnchor.absolute = ((GGPlayerSettings.instance.Model.handedness != 0) ? (-Mathf.Min(a, b)) : Mathf.Max(a, b));
	}

	public virtual void SetRanges(float skillDifference, float timing, float maxTiming, bool showThumb)
	{
		int num = 0;
		foreach (HitParamsDB.TimingRange range in ScriptableObjectSingleton<HitParamsDB>.instance.timing.ranges)
		{
			float f = Mathf.Min(range.timingRange.min, range.timingRange.max);
			if (!(Mathf.Sign(f) < 0f))
			{
				if (num >= layers.Count)
				{
					layers.Add(NGUITools.AddChild(layerPrefab.cachedTransform.parent.gameObject, layerPrefab.cachedGameObject).GetComponent<UISprite>());
				}
				UISprite uISprite = layers[num];
				uISprite.cachedGameObject.SetActive(true);
				uISprite.color = range.color;
				uISprite.depth = container.depth + 10 + ScriptableObjectSingleton<HitParamsDB>.instance.timing.ranges.Count - num;
				FloatRange skillDifferenceModifiedTimingRange = range.GetSkillDifferenceModifiedTimingRange(skillDifference);
				FloatRange realRangeInSec = ScriptableObjectSingleton<HitParamsDB>.instance.timing.GetRealRangeInSec(maxTiming);
				uISprite.GetComponent<QualityLayer>().SetDesiredHeight((int)((float)container.height * Mathf.Max(skillDifferenceModifiedTimingRange.min, skillDifferenceModifiedTimingRange.max)), Mathf.Max(skillDifferenceModifiedTimingRange.min, skillDifferenceModifiedTimingRange.max));
				num++;
			}
		}
		for (int i = num; i < layers.Count; i++)
		{
			layers[i].cachedGameObject.SetActive(false);
		}
		float normTiming = ScriptableObjectSingleton<HitParamsDB>.instance.timing.GetNormTiming(timing, maxTiming);
		Vector3 position = thumb.cachedTransform.position;
		Vector3[] worldCorners = container.worldCorners;
		int num2 = 0;
		int num3 = 1;
		position.y = Mathf.Lerp(worldCorners[num2].y, worldCorners[num3].y, MathEx.MapFromRangeToRange(1f, -1f, 0f, 1f, normTiming));
		thumb.cachedTransform.position = position;
		thumb.alpha = ((!showThumb) ? 0f : endAlpha);
	}

	public virtual void StartThumbAnimation()
	{
		if (base.gameObject.activeInHierarchy)
		{
			StopAllCoroutines();
			StartCoroutine(DoThumbAnimation(thumb, startScale, Vector3.one, startAlpha, endAlpha, thumbDuration));
		}
	}

	private IEnumerator DoThumbAnimation(UIWidget widget, Vector3 startScale, Vector3 endScale, float startAlpha, float endAlpha, float duration)
	{
		float time = 0f;
		while (time < duration)
		{
			time += RealTime.deltaTime;
			float normTime = time / duration;
			Vector3 scale = Vector3.Lerp(startScale, endScale, normTime);
			widget.cachedTransform.localScale = scale;
			widget.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
			yield return null;
		}
		widget.cachedTransform.localScale = endScale;
		widget.alpha = endAlpha;
	}
}
